//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client;

import tv.dyndns.kishibe.client.packet.PacketProblemData;
import tv.dyndns.kishibe.client.ui.WidgetMultiItemSelector;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PanelSearchProblem extends VerticalPanel implements ClickHandler, KeyDownHandler {
	private final Button buttonSearch = new Button("検索", this);
	private final TextBox textBoxQuery = new TextBox();
	private final TextBox textBoxCreater = new TextBox();
	private final WidgetMultiItemSelector multiItemSelectorGenre = new WidgetMultiItemSelector("ジャンル", Constant.NAMES_OF_GENRE, 3);
	private final WidgetMultiItemSelector multiItemSelectorType = new WidgetMultiItemSelector("出題形式", Constant.NAMES_OF_TYPE, 5);
	private final WidgetMultiItemSelector multiItemSelectorRandomFlag = new WidgetMultiItemSelector("ランダム", new String[] { "指定なし", "ランダム1", "ランダム2", "ランダム3", "ランダム4", "ランダム5" }, 3);
	private final ListBox listBoxMaxProblemsPerPage = new ListBox();
	private final SimplePanel panelGrid = new SimplePanel();
	private int maxProblemsPerPage;

	public PanelSearchProblem() {
		setWidth("800px");
		setHorizontalAlignment(ALIGN_CENTER);

		add(new Label("問題の検索を行います"));

		{
			textBoxQuery.addKeyDownHandler(this);
			textBoxCreater.addKeyDownHandler(this);

			textBoxQuery.setWidth("200px");
			textBoxCreater.setWidth("200px");
			listBoxMaxProblemsPerPage.setWidth("200px");

			listBoxMaxProblemsPerPage.addItem("10");
			listBoxMaxProblemsPerPage.addItem("100");
			listBoxMaxProblemsPerPage.addItem("1000");
			listBoxMaxProblemsPerPage.setSelectedIndex(1);

			final Grid grid = new Grid(3, 2);
			grid.addStyleName("gridFrame");
			grid.addStyleName("gridFontNormal");
			grid.setHTML(0, 0, "問題文");
			grid.setWidget(0, 1, textBoxQuery);
			grid.setHTML(1, 0, "投稿者");
			grid.setWidget(1, 1, textBoxCreater);
			grid.setHTML(2, 0, "1ページあたりの表示問題数");
			grid.setWidget(2, 1, listBoxMaxProblemsPerPage);
			add(grid);
		}
		add(multiItemSelectorGenre);
		add(multiItemSelectorType);
		add(multiItemSelectorRandomFlag);

		add(buttonSearch);
		add(panelGrid);
	}

	private void search() {
		if (SharedData.getInstance().getIsPlaying()) {
			return;
		}

		String query = textBoxQuery.getText();
		if (query == null) {
			query = "";
		}
		query = query.replaceAll("  ", " ");

		String creater = textBoxCreater.getText();
		if (creater == null) {
			creater = "";
		}
		creater = creater.replaceAll("　", " ");

		maxProblemsPerPage = Integer.parseInt(listBoxMaxProblemsPerPage.getItemText(listBoxMaxProblemsPerPage.getSelectedIndex()));

		setEnabled(false);

		final int genre = multiItemSelectorGenre.get();
		final int type = multiItemSelectorType.get();
		final int randomFlag = multiItemSelectorRandomFlag.get();

		Service.Util.getInstance().searchProblem(query, creater, genre, type, randomFlag, callbackSearchProblem);
	}

	private final AsyncCallback<PacketProblemData[]> callbackSearchProblem = new AsyncCallback<PacketProblemData[]>() {
		public void onSuccess(PacketProblemData[] result) {
			panelGrid.setWidget(new WidgetProblemReport(result, true, true, maxProblemsPerPage));
			setEnabled(true);
		}

		public void onFailure(Throwable caught) {
			caught.printStackTrace();
			setEnabled(true);
		}
	};

	private void setEnabled(boolean enabled) {
		buttonSearch.setEnabled(enabled);
		textBoxQuery.setEnabled(enabled);
		textBoxCreater.setEnabled(enabled);
		multiItemSelectorGenre.setEnabled(enabled);
		multiItemSelectorType.setEnabled(enabled);
		multiItemSelectorRandomFlag.setEnabled(enabled);
		listBoxMaxProblemsPerPage.setEnabled(enabled);
	}

	@Override
	public void onClick(ClickEvent event) {
		final Object sender = event.getSource();
		if (sender == buttonSearch) {
			search();
		}
	}

	@Override
	public void onKeyDown(KeyDownEvent event) {
		final Object sender = event.getSource();
		if (sender == textBoxQuery || sender == textBoxCreater) {
			if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
				search();
			}
		}
	}
}
